import { AstContext } from "../context"
import { AstVisitor } from "../visitor"

export class RoomInfoVisitor extends AstVisitor {
    visitRelativeLayout(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        if (id) {
            if (id.endsWith('live_bottom_bar_single_icon_container') && node.clickable() && node.visibleToUser()) {
                ctx.set('openShare', () => {
                    node.click()
                    node.recycle()
                })
                return true;
            }
        }
        return false;
    }
    visitImageView(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        if (id) {
            // live_close
            if (id.endsWith('live_close')) {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    ctx.set('closeRoom', () => {
                        clickable.click()
                        clickable.recycle()
                    })
                    return true;
                }
            }
        }
        return false;
    }
    // className("android.widget.FrameLayout")
    visitFrameLayout(node: AutoJs.UiObject, ctx: AstContext) {
        // id("live_audience_clearable_close_container")
        const id = node.id()
        if (id) {
            if (id.endsWith('live_audience_clearable_close_container')) {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    ctx.set('closeRoom', () => {
                        clickable.click()
                        clickable.recycle()
                    })
                    return true;
                }
            }
        }
        return false;
    }
    visitTextView(node: AutoJs.UiObject, ctx: AstContext) {
        if(!node.visibleToUser()) return true;
        const text = node.text()
        if (text) {
            if (["退出直播间", "退出"].includes(text)) {
                node.parent()?.click()
            }
            if (text === "立即关注") {
                back()
            }
        }
        const id = node.id()
        if (id) {
            if (id.endsWith('live_play_closed_v3_title_text_view') && text === "直播已结束") {
                ctx.set('closed', true)
            }
            if (id.endsWith('live_name_text')) {
                ctx.set('name', text)
            }
            if (id.endsWith('live_audience_count_text')) {
                ctx.set('online', parseInt(text))
            }
            if (id.endsWith('live_red_packet_status_text')) {
                ctx.set('status', text)
                ctx.set('openTime', ctx.formatData(ctx.toSecondDate(ctx.tosecond(text))))
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    ctx.set('openRedPacket', () => {
                        clickable.click()
                        clickable.recycle()
                    })
                    return true;
                }
            }
        }
        return false
    }
}

export const roomInfoVisitor = new RoomInfoVisitor()